<template>
  <q-page class="cc-admin">

    <div class="col bg-white shadow-2 q-pa-md q-ma-sm">
      <q-table flat color="primary" class="cross_table" separator="cell" :columns="columns" :data="list" row-key="id" :pagination.sync="pagination" :visible-columns="group" @request="query" :rows-per-page-options="[10, 20, 50, 100]" selection="multiple" :selected.sync="selected">
        <template v-slot:top="table">
          <div class="row no-wrap full-width">
            <q-input clearable outlined dense placeholder="请输入关键字搜索" class="on-left" @input="query" debounce="500" v-model="key">
              <template #append>
                <q-btn flat round icon="search" color="primary" @click="query" :loading="loading">
                  <q-tooltip>搜索</q-tooltip>
                </q-btn>
              </template>
            </q-input>
            <q-space />
            <template>
              <div class="q-gutter-xs">
                <q-btn icon="add" no-wrap color="primary" label="新建" @click="add" />
                <slot />
              </div>
            </template>
          </div>
        </template>
        <template #body-cell-opt="props">
          <q-td :props="props" :auto-width="true">
            <q-btn flat round dense color="info" icon='mdi-text-box-search-outline' @click="viewForm(props.row)">
              <q-tooltip>查看</q-tooltip>
            </q-btn>

            <q-btn flat round dense color="primary" icon="edit" @click="edit(props.row)"> <q-tooltip>编辑</q-tooltip></q-btn>
            <btn-del label="航线预测表" @confirm="del(props.row)" />
          </q-td>
        </template>
      </q-table>
    </div>
    <q-dialog maximized flat persistent ref="dialog">
      <q-form @submit="submit" class="dialog_card column">
        <h5 class="view_title justify-between q-px-md">
          {{ editType }}航线预测表
          <q-btn dense outline round icon="clear" size="sm" v-close-popup />
        </h5>
        <q-scroll-area class="col">
          <div class="row q-col-gutter-x-md dialog_form q-pa-md">
            <div class="col-12">
              <h5> 名称：</h5>
              <q-input outlined dense v-model="form.name" type="text" />
            </div>
            <div class="col-12">
              <div class="dynamic-form-item ">
                <div v-show="true" class="dynamic-form-item-label" style="width:100%;">
                  <q-icon name="star" color="red" v-show="false" />
                  平台
                </div>
                <select-search :formData="form" formKey="platformId" dense debounce="500" hide-bottom-space outlined :options="platformIdOptions" v-model="form.platformId" type="select" lazy-rules :rules="[]" clearable @input="platformIdInput(form.platformId, form)" />
              </div>
            </div>
            <div class="q-pa-sm scroll col-12">
              <q-table flat color="primary" class="cross_table" separator="cell" :columns="subcolumns" :data="weaPredictRrouteDetail" row-key="id" :rows-per-page-options="[10, 20, 50, 100]" :loading="loading" hide-bottom table-style="">

                <template #body-cell-index="props">
                  <q-td :props="props">{{ props.value }}</q-td>
                </template>
                <template #header-cell-recordTime="props">
                  <q-th :props="props">
                    {{ props.col.label }}
                  </q-th>
                </template>
                <template #body-cell-recordTime="props">
                  <q-td :props="props">
                    <input-date-picker dense debounce="500" hide-bottom-space outlined v-model="props.row.recordTime" type="date" :form="form" beginDate="" endDate="" lazy-rules :rules="[]" />
                  </q-td>
                </template>
                <template #header-cell-lon="props">
                  <q-th :props="props">
                    {{ props.col.label }}
                  </q-th>
                </template>
                <template #body-cell-lon="props">
                  <q-td :props="props">
                    <q-input dense debounce="500" hide-bottom-space outlined v-model="props.row.lon" type="text" lazy-rules :rules="[]" autogrow />
                  </q-td>
                </template>
                <template #header-cell-lat="props">
                  <q-th :props="props">
                    {{ props.col.label }}
                  </q-th>
                </template>
                <template #body-cell-lat="props">
                  <q-td :props="props">
                    <q-input dense debounce="500" hide-bottom-space outlined v-model="props.row.lat" type="text" lazy-rules :rules="[]" autogrow />
                  </q-td>
                </template>
                <template #header-cell-opt="props">
                  <q-th :props="props">
                    {{ props.col.label }}
                  </q-th>
                </template>
                <template #body-cell-opt="props">
                  <q-td :props="props" :auto-width="true">
                    <btn-del label="子表" @confirm="delSub(weaPredictRrouteDetail, props.row)" />
                  </q-td>
                </template>

                <template v-slot:bottom-row>
                  <q-tr>
                    <q-td colspan="100%">
                      <div class="row">
                        <q-btn dense flat text-color="light-blue-13" label="新增一行" icon="add" @click="
                          addSub(weaPredictRrouteDetail, weaPredictRrouteFormDetail)" />
                      </div>
                    </q-td>
                  </q-tr>
                </template>

              </q-table>
            </div>

          </div>
        </q-scroll-area>
        <div class="row justify-center q-pa-md">
          <q-btn outline color="primary" icon="mdi-close-thick" label="关闭" v-close-popup />
          <q-btn class="q-mx-md" color="primary" icon="mdi-check-bold" label="提交" :loading="loading" type="submit" />
        </div>
      </q-form>
    </q-dialog>
    <confirm ref="confirmDialog" :msg="confirmMsg" @confirm="deleteBatch()" />
    <q-inner-loading :showing="loading">
      <q-spinner-gears size="50px" color="primary" />
    </q-inner-loading>
  </q-page>
</template>

<script>
import { IndexMixin } from 'boot/mixins';
import { getDictLabel } from 'boot/dictionary';
import drawline from 'components/map/drawline';
import confirm from 'components/confirm';

export default {
  name: 'WeaPredictRoute',
  mixins: [IndexMixin],
  components: {
    confirm,
    drawline,
  },
  data() {
    return {
      columns: [
        {
          name: 'index',
          align: 'center',
          label: '序号',
          field: 'index',
        },
        {
          name: 'name', align: 'left', label: '名称', field: 'name',
        },
        {
          name: 'platformIdName', align: 'left', label: '平台名称', field: 'platformIdName',
        },
        {
          name: 'opt', align: 'center', label: '操作', field: 'id',
        },
      ],
      weaPredictRrouteDetail: [],
      weaPredictRrouteFormDetail: {
        lon: null,
        lat: null,
        recordTime: null,
      },
      weaPredictRrouteEmptyFormDetail: {
        lon: null,
        lat: null,
        recordTime: null,
      },
      platformIdOptions: [],
      subcolumns: [
        {
        name: 'index',
        align: 'center',
        label: '序号',
        field: 'index',
        headerStyle: 'width: 80px;',
      }, {
        name: 'recordTime',
        align: 'center',
        label: '日期时间',
        field: 'recordTime',
        headerStyle: 'width: 300px;',
      }, {
        name: 'lon',
        align: 'center',
        label: '经度',
        field: 'lon',
        headerStyle: '',
      }, {
        name: 'lat',
        align: 'center',
        label: '纬度',
        field: 'lat',
        headerStyle: '',
      }, {
        name: 'opt',
        align: 'center',
        label: '操作',
        field: 'opt',
      },],
      form: {
        name: '',
        platformId: '',
        platformIdName: '',
      },
      emptyForm: {
        name: '',
        platformId: '',
        platformIdName: '',
      },
      showQuery: true,
      headers: [{ name: 'Authorization', value: localStorage.Authorization }],
      uploadUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/upload`,
      imgUrl: `${process.env.SERVER_URL}${process.env.BASE_URL}/sys/common/static`,
      url: {
        list: '/wea/predictRoute/list',
        add: '/wea/predictRoute/add',
        edit: '/wea/predictRoute/edit',
        copy:
          '/wea/predictRoute/copy',
        delete: '/wea/predictRoute/delete',
        deleteBatch: '/wea/predictRoute/deleteBatch',
        exportXlsUrl: '/wea/predictRoute/exportXls',
        importExcelUrl: '/wea/predictRoute/importExcel',
      },
    };
  },
  methods: {
    getDictLabel,
    addBefore() {
      this.form.path = '';
      this.weaPredictRouteDetailList = [];
      return true;
    },
    platformIdInput(v) {
      const opt = this.platformIdOptions.find((o) => o.value === v);
      if (opt) {
        this.form.platformIdName = opt.label;
      }
      if (!v) {
        this.form.platformIdName = '';
      }
    },
    submit() {
      this.form.weaPredictRouteDetailList = this.weaPredictRrouteDetail;
      let q;
      if (this.editType === '新建') {
        q = this.$axios.post(this.url.add, this.form);
      } else {
        q = this.$axios.put(this.url.edit, this.form);
      }
      return q.then((r) => {
        this.$info(r.message);
        this.$refs.dialog.hide();
      }).finally(() => {
        this.query();
      });
    },
    edit(p) {
      this.editType = '编辑';
      this.$axios.get('/wea/predictRoute/queryById', {
        params: {
          id: p.id,
        }
      }).then(({ result }) => {
        this.form = {
          ...result,
        };
        this.weaPredictRrouteDetail = result.weaPredictRouteDetailList;
        this.calcDataIndex(this.weaPredictRrouteDetail);
        this.$refs.dialog.show();
      });
    },
    delSub(dataList, row) {
      dataList.splice(dataList.findIndex((item) => item.index === row.index), 1);
      this.calcDataIndex(dataList);
    },
    initDict() {
      this.$axios.get('/sys/dictItem/selectItemsByDefId?defId=weaPlatform').then((r) => {
        this.platformIdOptions = r;
      });
    },
    addSub(dataList, subForm) {
      dataList.push({ ...subForm });
      this.calcDataIndex(dataList);
    },
    calcDataIndex(dataList) {
      if (dataList && dataList.length > 0) {
        dataList.map((v, i) => {
          v.index = i + 1;
          return v;
        });
      }
    },
    save() {
      const lineArray = this.$refs.drawLineCmp.getLine();
      if (!lineArray || lineArray.length !== 1) {
        this.$error('只能画一条线');
        return;
      }
      const line = lineArray[0];
      this.form.path = JSON.stringify(line);
      this.submit();
    },
    viewForm(row) {
      window.open(this.$router.resolve(`/map/predict?id=${row.id}`).href, '_blank');
    },
  },
  mounted() {
    this.initDict();
  },
};
</script>

<style lang="stylus">

</style>
